package com.cykj.page03;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page03
 * @Author: 阿隆
 * @CreateTime: 2025-10-18 14:36
 * @Description: TODO
 * @Version: 1.0
 */
public class OneService {
    public static void main(String[] args) {
        try {
            //创建ServerSocket => 服务器
            ServerSocket serverSocket = new ServerSocket(9090);

            System.out.println("服务端启动端口号为: 9090");

            //等待客户端连接  阻塞
            Socket socket = serverSocket.accept();

            //读取客户端发送的消息
            InputStream is = socket.getInputStream();

            byte[] bytes = new byte[1024];

            //读取
            int len = is.read(bytes);

            System.out.println(new String(bytes, 0, len));

            String msg = "浏览器你好我接受到数据了01浏览器你好我接受到数据了";

            //给浏览器响应消息
            OutputStream os = socket.getOutputStream();
            os.write("HTTP/1.1 200 OK".getBytes());
            os.write("\r\n".getBytes());  //回车换行
            os.write("Content-Type: text/html; charset=utf-8".getBytes());
            os.write("\r\n".getBytes());  //回车换行
            os.write(("Content-Length: " + msg.getBytes().length).getBytes()); //大小 字节大小
            os.write("\r\n".getBytes());  //回车换行
            os.write("\r\n".getBytes());  //回车换行
            os.write(msg.getBytes());


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
